package com.jfinal.weixin.demo.youjia;

import com.alibaba.fastjson.JSON;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * http://apistore.baidu.com/apiworks/servicedetail/710.html
 * Created by wangxiaoyang on 16/8/28.
 */
public class YoujiaApi {

    private static final String baseUrl = "http://apis.baidu.com/showapi_open_bus/oil_price/find?prov=";

    public static void main(String[] args) {
        getYoujia(new OkHttpClient(),"湖北黄冈");
    }

    public static YoujiaBean getYoujia(OkHttpClient httpClient, String prov) {
        String url = baseUrl + prov;
        Request request = new Request.Builder()
                .url(url)
                .addHeader("apikey", "1f1caef2b32c608aa9c63ac0965410da")
                .build();
        try {
            Response response = httpClient.newCall(request).execute();
            String result = response.body().string();
            System.out.println("result1 \n" + result);
            YoujiaBean youjiaBean = JSON.parseObject(result,YoujiaBean.class);
            System.out.println("youjiaBean " + youjiaBean.format());
            return youjiaBean;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
